package jp.sourceforge.akjrcp.greycstoration;


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import jp.sourceforge.akjrcp.akj_betas.Akj_betasPlugin;
import jp.sourceforge.akjrcp.dropsomething.DirectoryBasedExecuter;
import jp.sourceforge.akjrcp.dropsomething.DropsomethingView;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegFLVFileEffector;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegUI;
import jp.sourceforge.akjrcp.ui.DirectryBasedExecuterListener;
import jp.sourceforge.akjrcp.util.PathnameUtils;
import jp.sourceforge.akjrcp.util.ProcessMonitor;
import jp.sourceforge.akjrcp.util.ProcessPrinter;
import jp.sourceforge.akjrcp.util.image.ImageUtils;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.WorkbenchWindow;

public class DirectoryBasedGreycstorationExecuter implements DirectoryBasedExecuter{



	public static Log log = LogFactory.getLog(DirectoryBasedGreycstorationExecuter.class);
	
	private DirectryBasedExecuterListener executeListener;

	private String exportDir;
	private boolean sameDirectory;
	private boolean backup;
	
	RestorationSetting restorationSetting;

	private Process process;	
	private boolean cancelling;	//for cancel

	private String settingLabel;
	
	private GFFMpegFLVFileEffector effector;
	

	private boolean createCompareImage;
	RepeatImageSetting repeatImageSetting=new RepeatImageSetting();
	public class RepeatImageSetting{
		private boolean enabled;
		private String paramName;
		private double incrementValue;
		private int repeatTime;
		public boolean isEnabled() {
			return enabled;
		}
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		public double getIncrementValue() {
			return incrementValue;
		}
		public void setIncrementValue(double incrementValue) {
			this.incrementValue = incrementValue;
		}
		public String getParamName() {
			return paramName;
		}
		public void setParamName(String paramName) {
			this.paramName = paramName;
		}
		public int getRepeatTime() {
			return repeatTime;
		}
		public void setRepeatTime(int repeatTime) {
			this.repeatTime = repeatTime;
		}
	}
	
	public void execute(File files[]){
		if(executeListener!=null){
			executeListener.beforeConvert();
		}
		
		for (int i = 0; i < files.length; i++) {
			execute(files[i]);
		}
		
		if(executeListener!=null){
			executeListener.afterConvert();
		}
	}

	private void execute(File file){
		if(file.isDirectory()){
			
			if(cancelling){
				log.info("cancel "+file);
				return;
			}
			
			String list[]=file.list();
			if(list!=null){
				for (int i = 0; i < list.length; i++) {
					execute(new File(file,list[i]));
				}
			}
		}else{
			if(accept(file.getParentFile(),file.getName())){
			if(isFLV(file)){
			inExecuteMovie(file);
			}else{
			inExecute(file);// do only  accepted file.
			}
			}
		}
	}

	public void cancel(){
		log.info("call cancel");
		cancelling=true;
		if(process!=null){
			process.destroy();
		}
		if(effector!=null){
			effector.cancel();
		}
		
	}
	
	public void setStatusValue(String value){
		PlatformUI.getWorkbench().getDisplay().asyncExec(new SetMessage(value));
		
	}
	public class SetMessage implements Runnable{
		String message;
		SetMessage(String message){
			this.message=message;
		}
		public void run(){
			WorkbenchWindow workbenchWindow =  (WorkbenchWindow) DropsomethingView.viewSite.getWorkbenchWindow();
			IActionBars actionBars = workbenchWindow.getActionBars();
			IStatusLineManager manager = actionBars.getStatusLineManager();
			manager.setMessage(message);
		}
	}
	
	public static final ProcessTimeout getProcessTimeout(){
		if(processTimeout==null){
			processTimeout=new ProcessTimeout();
			processTimeout.start();//never stop;
		}
		return processTimeout;
	}
	private static ProcessTimeout processTimeout;
	private static class ProcessTimeout extends Thread{
		private Process timeoutProcess;
		private long lastmodified;
		private long timeout=1000*120;
		private boolean monitering=true;
		public void update(Process process){
			this.timeoutProcess=process;
			lastmodified=System.currentTimeMillis();
		}
		public void run(){
			while(monitering){
			if(timeoutProcess!=null){
				if(System.currentTimeMillis()>lastmodified+timeout){
					log.warn("process destroyed by timeout");
					timeoutProcess.destroy();//kill stopped process.
					monitering=false;
					processTimeout=null;//done.
				}else{
					log.info(timeoutProcess+" is OK");
				}
			}
				
			
			try {
				Thread.sleep(timeout);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
		}
	}
	public class GFFMpegFLVFileEffector extends FFMpegFLVFileEffector{

		;
		private ProcessPrinter printer;


		public GFFMpegFLVFileEffector(String ffmpegPath, File input, File output) {
			super(ffmpegPath, input, output);
			//log.info(ArrayUtils.toString(commands));
			printer = new ErrorCatchProcessPrinter();
			/*
			try {
				printer.printOutput(toDebugArray(commands));
			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}*/
			
		}
		
		
		//should overwrite
		private boolean debug_copy_only=false;
		
		private int maxtrycound=5;
		public void effect(File inputFile,File outputFile){
			
			int trycount=0;
			try {
				
				//if(FFMpegFLVFileEffector.doneFrames<810){
					//for debug.
				if(debug_copy_only){
					//Thread.sleep(5000);
					FileUtils.copyFile(inputFile, outputFile);
					return;
				}
				
			} catch (Exception e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}/** for debug*/
			
			 String[] commands = restorationSetting.toCommand(inputFile,outputFile);
			
//				
				
				
				Runtime runtime=Runtime.getRuntime();

				
				
				for(int i=0;i<maxtrycound;i++){
				if(i!=0){
					log.warn((i+1)+"nd time try convert:"+ArrayUtils.toString(commands));
				}
				process = null;
				try {
					process = runtime.exec(commands);
					getProcessTimeout().update(process);//wait this process. //this method for grey command faild.
					ProcessMonitor monitor=new ProcessMonitor(process,printer);
					monitor.startMonitor();
					
					try {
						
						process.waitFor();
						int result=process.exitValue();
						if(result==0){
							return;
						}else{
							log.warn("exit values is wrong:"+result);
						}
						
					} catch (InterruptedException e) {
						
						e.printStackTrace();
					}
					
				} catch (IOException e1) {
					//maybe catch not supported codecs.
					//or timeout
					e1.printStackTrace();
					process.destroy();
					log.info("killed process");
					//return;
				}finally{

				}
			
				}
				
				//faild.
				if(executeListener!=null){
					executeListener.faildConvert(inputFile,"all time try faild");
					}
		}

	}
	
	private void inExecuteMovie(File inputFile){
		if(cancelling){
			log.info("cancel "+inputFile);
			return;
		}
		if(executeListener!=null){
			executeListener.startConvert(inputFile);
		}
		String outputDir=inputFile.getParent();
		if(!sameDirectory){
			outputDir=exportDir;
		}
		
		//boolean sameInputAndOutput=false;
			
		//String remain=PathnameUtils.getRemainPath(inputFile.getParent(),PathnameUtils.changeExtension(inputFile.getAbsolutePath(),ffmpegPreference.getExtension()));
		String outputPath=outputDir+"\\"+restorationSetting.toFileName(inputFile.getName());//don't keep base directory name.i don't know reason why,TODO:change these.
		
		log.info(inputFile.getAbsolutePath());
		log.info(outputPath);
		
	
		
		//if input and output same return;
		File outPutFile=new File(outputPath);
		File tmpOutput=null;
		try {
			tmpOutput = File.createTempFile("grey",inputFile.getName());
		} catch (IOException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}
		
		/*
		if(inputFile.getAbsolutePath().equals(outputPath)){
			sameInputAndOutput=true;
		}*/
		// check if directory is realy only?
		
		// check if file can rename to backup.
		boolean overwrite=false;
		if(outPutFile.exists() && !outPutFile.canWrite()){
			if(!outPutFile.exists() && executeListener!=null){
				executeListener.faildConvert(inputFile,DirectryBasedExecuterListener.ERROR_CANT_OVERWRITE);
				return;
			}
		}
		
		//ask overwrite
		if(!backup){
		if(outPutFile.exists() && executeListener!=null){
			overwrite=executeListener.askOverwrite(tmpOutput);//never ask?
			if(!overwrite){
				if(executeListener!=null){
				executeListener.faildConvert(inputFile,DirectryBasedExecuterListener.ERROR_DONT_OVERWRITE);
				}
				return;
			}
		}
		}
		
		
		String ffmpegPath=FFMpegUI.getFFmpeg();
		if(ffmpegPath==null){
			executeListener.faildConvert(inputFile,DirectryBasedExecuterListener.ERROR_FFMPEG_NOT_FOUND);
			return;
		}
		
		effector = new GFFMpegFLVFileEffector(ffmpegPath,inputFile,tmpOutput);
		effector.setInputImageType(FFMpegFLVFileEffector.IMAGE_BMP);
		effector.setOutputImageType(FFMpegFLVFileEffector.IMAGE_BMP);
		effector.start();
		long starttime=System.currentTimeMillis();
		while(!effector.isDoneRead() || !effector.isDoneWrite()){
			
			try {
				Thread.sleep(1000*3);
				setStatusValue(FFMpegFLVFileEffector.doneFrames+"frames done.it's about "+(FFMpegFLVFileEffector.doneFrames/30)+"second");
			} catch (InterruptedException e) {
				
				e.printStackTrace();
			}
		}
		log.info("***finally finished***");
		long end=System.currentTimeMillis()-starttime;
		String min=""+(end/1000/60);
		log.info("time:"+min);
		try {
			Thread.sleep(1000*3);
		} catch (InterruptedException e) {
		
			e.printStackTrace();
		}

		
		//if there are file?
		//if i should backup rename old file to backup
		if(outPutFile.exists()){
			if(backup){
			File newBackupFile=PathnameUtils.getEmptyFileName(outPutFile);
			boolean renamed=outPutFile.renameTo(newBackupFile);
			if(renamed){
				boolean renamed2=tmpOutput.renameTo(new File(outputPath));
				if(!renamed2){
					if(executeListener!=null){
						tmpOutput.delete();
						executeListener.faildConvert(inputFile,DirectryBasedExecuterListener.ERROR_CANT_OVERWRITE);
						return;
					}
				}
			}else{
				if(executeListener!=null){
					tmpOutput.delete();
					executeListener.faildConvert(inputFile,DirectryBasedExecuterListener.ERROR_CANT_OVERWRITE);
					return;
				}
			}}else{
				outPutFile.delete();// don't need backup.
				boolean renamed2=tmpOutput.renameTo(new File(outputPath));
				if(!renamed2){
					if(executeListener!=null){
						tmpOutput.delete();
						executeListener.faildConvert(inputFile,DirectryBasedExecuterListener.ERROR_CANT_OVERWRITE);
						return;
					}
				}
			}
		}else{
			//there are no output file and just rename it.
			boolean renamed2=tmpOutput.renameTo(new File(outputPath));
			if(!renamed2){
				if(executeListener!=null){
					tmpOutput.delete();
					executeListener.faildConvert(inputFile,DirectryBasedExecuterListener.ERROR_CANT_OVERWRITE);
					return;
				}
			}
		}
		
		
		//call finish
		if(executeListener!=null){
			executeListener.endConvert(inputFile);
			}
		effector=null;
		process=null;
		cancelling=false;
	}
	//public class ImageIterator 
	//input output must be same extension
	public void createRestorateImage(File inputFile,File outputFile,RestorationSetting restorationSetting) throws IOException{
//		execute command
		//tmp file formats.
		File exeInput=null;
		File exeOutput=null;
		boolean usingTemp=false;
		if(isBMP(inputFile)){
			exeInput=inputFile;
			exeOutput=outputFile;
		}else{
			try {
				usingTemp=true;
				exeInput=File.createTempFile("temp", ".bmp");
				
				Image image=new Image(null,inputFile.getAbsolutePath());

				
				ImageLoader loader=new ImageLoader();
				loader.data=new ImageData[]{image.getImageData()};
				loader.save(exeInput.getAbsolutePath(), SWT.IMAGE_BMP);
				
				log.trace(exeInput.getAbsolutePath());
				
				exeOutput=File.createTempFile("temp", ".bmp");
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		}
		String[] commands=restorationSetting.toCommand(exeInput,exeOutput);
		log.info(ArrayUtils.toString(commands));
		Runtime runtime=Runtime.getRuntime();

		ProcessPrinter printer=new ErrorCatchProcessPrinter();
		try {
			printer.printOutput(toDebugArray(commands));
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		process = null;
		try {
			process = runtime.exec(commands);
			ProcessMonitor monitor=new ProcessMonitor(process,printer);
			monitor.startMonitor();
			
			try {
				process.waitFor();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (IOException e1) {
			//maybe catch not supported codecs.
			
			process.destroy();
			if(usingTemp){
				exeInput.delete();
				exeOutput.delete();
			}
			throw e1;
			
		}
		if(usingTemp){//jpeg or png
			try {
				BufferedImage image=ImageIO.read(exeOutput);
				if(image==null){
					exeInput.delete();
					exeOutput.delete();
					throw new IOException("image is null");
				}
				
				if(isJPEG(outputFile)){
					ImageUtils.writeJpegWithQuality(image,outputFile,1.0f);
				}else if(isPNG(outputFile)){
					ImageIO.write(image, "PNG", outputFile);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//remove tmp files
			exeInput.delete();
			exeOutput.delete();
		}
	}
	private void inExecute(File inputFile){
		long starttime=System.currentTimeMillis();
		if(cancelling){
			log.info("cancel "+inputFile);
			return;
		}
		if(executeListener!=null){
			executeListener.startConvert(inputFile);
		}
		String outputDir=inputFile.getParent();
		if(!sameDirectory){
			outputDir=exportDir;
		}
		
		//boolean sameInputAndOutput=false;
			
		//String remain=PathnameUtils.getRemainPath(inputFile.getParent(),PathnameUtils.changeExtension(inputFile.getAbsolutePath(),ffmpegPreference.getExtension()));
		//String outputPath=outputDir+"\\"+inputFile.getName();//don't keep base directory name.i don't know reason why,TODO:change these.
		
		String outputPath=outputDir+"\\"+restorationSetting.toFileName(inputFile.getName());//don't keep base directory name.i don't know reason why,TODO:change these.
		
		log.info(inputFile.getAbsolutePath());
		log.info(outputPath);
		
		if(repeatImageSetting.isEnabled()){
			//make final image.
			RepeatBufferedImageMaker repeatBufferedImageMaker=new RepeatBufferedImageMaker(repeatImageSetting.getRepeatTime(),settingLabel);
			for(int i=0;i<repeatImageSetting.getRepeatTime();i++){
				
				try {
					//convert;
					File tmpImage=File.createTempFile("grey", inputFile.getName());
					createRestorateImage(inputFile, tmpImage, restorationSetting);
					//copy image.
					BufferedImage drawImage=ImageIO.read(tmpImage);
					repeatBufferedImageMaker.paintImage(drawImage, restorationSetting.getParamLabel(repeatImageSetting.getParamName()));
					drawImage=null;
				} catch (IOException e) {
					
					e.printStackTrace();
					if(executeListener!=null){
						executeListener.faildConvert(inputFile,DirectryBasedExecuterListener.ERROR_CANT_TEMP);
						}
						return;
				}
				
				
				//refresh.
				System.gc();
				//change setting
				restorationSetting.incrementValue(repeatImageSetting.getParamName(), repeatImageSetting.getIncrementValue());
			}
			//save final image.
			File repeatFile=new File(new File(outputPath).getParentFile(),PathnameUtils.removeExtension(inputFile.getName())+"_repeat.png");//TODO paramname
			try {
				ImageIO.write(repeatBufferedImageMaker.getBufferedImage(), "PNG", repeatFile);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				if(executeListener!=null){
					executeListener.faildConvert(inputFile,DirectryBasedExecuterListener.ERROR_CANT_OUTPUT+" "+repeatFile.getAbsolutePath());
					}
					return;
				
			}
		}else{
		//if input and output same return;
		File outPutFile=new File(outputPath);
		File tmpOutput=null;
		try {
			tmpOutput = File.createTempFile("grey",inputFile.getName());
		} catch (IOException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}
		
		/*
		if(inputFile.getAbsolutePath().equals(outputPath)){
			sameInputAndOutput=true;
		}*/
		// check if directory is realy only?
		
		// check if file can rename to backup.
		boolean overwrite=false;
		if(outPutFile.exists() && !outPutFile.canWrite()){
			if(!outPutFile.exists() && executeListener!=null){
				executeListener.faildConvert(inputFile,DirectryBasedExecuterListener.ERROR_CANT_OVERWRITE);
				return;
			}
		}
		
		//ask overwrite
		if(!backup){
		if(outPutFile.exists() && executeListener!=null){
			overwrite=executeListener.askOverwrite(tmpOutput);//never ask?
			if(!overwrite){
				if(executeListener!=null){
				executeListener.faildConvert(inputFile,DirectryBasedExecuterListener.ERROR_DONT_OVERWRITE);
				}
				return;
			}
		}
		}
		
		//execute command
		//tmp file formats.
		File exeInput=null;
		File exeOutput=null;
		boolean usingTemp=false;
		if(isBMP(inputFile)){
			exeInput=inputFile;
			exeOutput=tmpOutput;
		}else{
			try {
				usingTemp=true;
				exeInput=File.createTempFile("temp", ".bmp");
				
				Image image=new Image(null,inputFile.getAbsolutePath());

				
				ImageLoader loader=new ImageLoader();
				loader.data=new ImageData[]{image.getImageData()};
				loader.save(exeInput.getAbsolutePath(), SWT.IMAGE_BMP);
				
				log.trace(exeInput.getAbsolutePath());
				
				exeOutput=File.createTempFile("temp", ".bmp");
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		}
		String[] commands=restorationSetting.toCommand(exeInput,exeOutput);
		log.info(ArrayUtils.toString(commands));
		Runtime runtime=Runtime.getRuntime();

		ProcessPrinter printer=new ErrorCatchProcessPrinter();
		try {
			printer.printOutput(toDebugArray(commands));
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		process = null;
		try {
			process = runtime.exec(commands);
			ProcessMonitor monitor=new ProcessMonitor(process,printer);
			monitor.startMonitor();
			
			try {
				process.waitFor();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (IOException e1) {
			//maybe catch not supported codecs.
			e1.printStackTrace();
			process.destroy();
			if(executeListener!=null){
				executeListener.faildConvert(inputFile,e1.getMessage());
				}
			return;
		}finally{

		}
		if(usingTemp){//jpeg or png
			try {
				BufferedImage image=ImageIO.read(exeOutput);
				if(image==null && executeListener!=null){
					tmpOutput.delete();
					executeListener.faildConvert(inputFile,DirectryBasedExecuterListener.ERROR_CANT_OVERWRITE);
					exeInput.delete();
					exeOutput.delete();
					return;
				}
				//TODO: if image is null.
				if(isJPEG(tmpOutput)){
					ImageUtils.writeJpegWithQuality(image,tmpOutput,1.0f);
				}else if(isPNG(tmpOutput)){
					ImageIO.write(image, "PNG", tmpOutput);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//remove tmp files
			exeInput.delete();
			exeOutput.delete();
		}
		//bmp direct save.
		
		//if there are file?
		//if i should backup rename old file to backup
		if(outPutFile.exists()){
			if(backup){
			File newBackupFile=PathnameUtils.getEmptyFileName(outPutFile);
			boolean renamed=outPutFile.renameTo(newBackupFile);
			if(renamed){
				boolean renamed2=tmpOutput.renameTo(new File(outputPath));
				if(!renamed2){
					if(executeListener!=null){
						tmpOutput.delete();
						executeListener.faildConvert(inputFile,DirectryBasedExecuterListener.ERROR_CANT_OVERWRITE);
						return;
					}
				}
			}else{
				if(executeListener!=null){
					tmpOutput.delete();
					executeListener.faildConvert(inputFile,DirectryBasedExecuterListener.ERROR_CANT_OVERWRITE);
					return;
				}
			}}else{
				outPutFile.delete();// don't need backup.
				boolean renamed2=tmpOutput.renameTo(new File(outputPath));
				if(!renamed2){
					if(executeListener!=null){
						tmpOutput.delete();
						executeListener.faildConvert(inputFile,DirectryBasedExecuterListener.ERROR_CANT_OVERWRITE);
						return;
					}
				}
			}
		}else{
			boolean renamed2=tmpOutput.renameTo(new File(outputPath));
			if(!renamed2){
				if(executeListener!=null){
					tmpOutput.delete();
					executeListener.faildConvert(inputFile,DirectryBasedExecuterListener.ERROR_CANT_OVERWRITE);
					return;
				}
			
			}
		}
		
		//make before after
		if(createCompareImage){
		//TODO overwrite?
		File outputFile=new File(outputPath);
		File before_after=new File(outputFile.getParentFile(),PathnameUtils.removeExtension(inputFile.getName())+"_before_after.png");
		try {
			BufferedImage image_before=ImageIO.read(inputFile);
			BufferedImage image_after=ImageIO.read(outputFile);
			BeforeAfterBufferedImageMaker bm=new BeforeAfterBufferedImageMaker();
			bm.setDescription(settingLabel);
			BufferedImage before_after_image=bm.createBufferedImage(image_before, image_after);
			ImageIO.write(before_after_image, "PNG", before_after);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		
		}
		
		//done
		long end=System.currentTimeMillis()-starttime;
		String sec=""+(end/1000);
		log.info("time:"+sec+"s");
		
		//call finish
		if(executeListener!=null){
			executeListener.endConvert(inputFile);
			}
		
		process=null;
		cancelling=false;
	}

	
	public static String toDebugArray(String[] array){
		String v="";
		for (int i = 0; i < array.length; i++) {
			v+="\""+array[i]+"\""+" ";
		}
		return v+"\n";
	}

	public boolean isBackup() {
		return backup;
	}

	public void setBackup(boolean backup) {
		this.backup = backup;
	}

	public DirectryBasedExecuterListener getExecuteListener() {
		return executeListener;
	}

	public void setExecuteListener(DirectryBasedExecuterListener executeListener) {
		this.executeListener = executeListener;
	}

	public String getExportDir() {
		return exportDir;
	}

	public void setExportDir(String exportDir) {
		this.exportDir = exportDir;
	}




	public boolean isSameDirectory() {
		return sameDirectory;
	}

	public void setSameDirectory(boolean sameDirectory) {
		this.sameDirectory = sameDirectory;
	}



	public class ErrorCatchProcessPrinter implements ProcessPrinter{
		public void printOutput(String output) throws IOException {
			print(output);
		}

		public void printError(String error)  throws IOException{
			print(error);
		}

		private void print(String line) throws IOException{
			if(executeListener!=null){
				executeListener.printProcess(line);
				}
			
			//log.debug(line);
			
			//if(line.indexOf(DirectryBasedExecuterListener.ERROR_NOTSUPPORTED_VIDEO)!=-1){
			//	throw new IOException(DirectryBasedExecuterListener.ERROR_NOTSUPPORTED_VIDEO);
			//}
			
		}
		
	}
	
	//default path all file
	public boolean accept(File parent,String name){
		String[] supportImages=new String[]{"jpg","bmp","png","flv"};
		for (int i = 0; i < supportImages.length; i++) {
			if(name.toLowerCase().endsWith("."+supportImages[i])){
				return true;
			}
		}
		return false;
	}
	
	public boolean isFLV(File file){
		return file.getName().toLowerCase().endsWith(".flv");
	}

	public boolean isBMP(File file){
		return file.getName().toLowerCase().endsWith(".bmp");
	}
	
	public boolean isJPEG(File file){
		return file.getName().toLowerCase().endsWith(".jpg");
	}
	
	public boolean isPNG(File file){
		return file.getName().toLowerCase().endsWith(".png");
	}

	public RestorationSetting getRestorationSetting() {
		return restorationSetting;
	}

	public void setRestorationSetting(RestorationSetting restorationSetting) {
		this.restorationSetting = restorationSetting;
	}

	public String getSettingLabel() {
		return settingLabel;
	}

	public void setSettingLabel(String settingLabel) {
		this.settingLabel = settingLabel;
	}

	public boolean isCreateCompareImage() {
		return createCompareImage;
	}

	public void setCreateCompareImage(boolean createCompareImage) {
		this.createCompareImage = createCompareImage;
	}
	
}
